package com.jxt.myjxtmanager.ui.model;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.jxt.myjxtmanager.entity.AskForLeaveBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qm on 2017/3/13 0013.
 */
public class AskLeaveMo {
    /**
     * 总数据
     */
    private List<AskForLeaveBean> queryList;
    private final Context context;
    private OnFindAllDataListener onFindAllDevicesListener;

    private String strStudent = "{\n" +
            "    \"code\": 1,\n" +
            "    \"error_code\": 0,\n" +
            "    \"is_msg\": 0,\n" +
            "    \"result\": [\n" +
            "        {\n" +
            "            \"id\": \"1000223052\",\n" +
            "            \"leaveTime\": 2017-02-05,\n" +
            "            \"leaveReason\": 生病请假,\n" +
            "            \"leaveHowLong\": 2,\n" +
            "            \"leaveCount\": 2,\n" +
            "            \"type\": \"\",\n" +
            "             \"name\":李小明\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": \"1000223052\",\n" +
            "            \"leaveTime\": 2017-01-28,\n" +
            "            \"leaveReason\": 哥哥结婚,\n" +
            "            \"leaveHowLong\": 1,\n" +
            "            \"leaveCount\": 1,\n" +
            "            \"type\": \"\",\n" +
            "             \"name\":胡小明\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": \"1000223053\",\n" +
            "            \"leaveTime\": 2016-11-05,\n" +
            "            \"leaveReason\": 回老家,\n" +
            "            \"leaveHowLong\": 3,\n" +
            "            \"leaveCount\": 0,\n" +
            "            \"type\": \"\",\n" +
            "             \"name\":张小明\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": \"1000223054\",\n" +
            "            \"leaveTime\": 2016-09-15,\n" +
            "            \"leaveReason\": 想出去玩,\n" +
            "            \"leaveHowLong\": 2,\n" +
            "            \"leaveCount\": 2,\n" +
            "            \"type\": \"\",\n" +
            "             \"name\":刘小明\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": \"1000223055\",\n" +
            "            \"leaveTime\": 2016-05-14,\n" +
            "            \"leaveReason\": 最近学习太累了，想出去散散心,\n" +
            "            \"leaveHowLong\": 2,\n" +
            "            \"leaveCount\": 5,\n" +
            "            \"type\": \"\",\n" +
            "             \"name\":马小明\n" +
            "        }\n" +
            "    ],\n" +
            "    \"msg\": null,\n" +
            "    \"success\": true,\n" +
            "    \"failed\": false,\n" +
            "    \"error\": false\n" +
            "}";
    private String str = "{\n" +
            "    \"code\": 1,\n" +
            "    \"error_code\": 0,\n" +
            "    \"is_msg\": 0,\n" +
            "    \"result\": [\n" +
            "        {\n" +
            "            \"id\": \"1000223052\",\n" +
            "            \"leaveTime\": 2017-02-05,\n" +
            "            \"leaveReason\": 生病请假,\n" +
            "            \"leaveHowLong\": 2,\n" +
            "            \"type\": 通过\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": \"1000223052\",\n" +
            "            \"leaveTime\": 2017-01-28,\n" +
            "            \"leaveReason\": 私事,\n" +
            "            \"leaveHowLong\": 1,\n" +
            "            \"type\": 通过\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": \"1000223052\",\n" +
            "            \"leaveTime\": 2016-11-05,\n" +
            "            \"leaveReason\": 回老家,\n" +
            "            \"leaveHowLong\": 3,\n" +
            "            \"type\": 通过\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": \"1000223052\",\n" +
            "            \"leaveTime\": 2016-09-15,\n" +
            "            \"leaveReason\": 想出去玩,\n" +
            "            \"leaveHowLong\": 2,\n" +
            "            \"type\": 驳回\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": \"1000223052\",\n" +
            "            \"leaveTime\": 2016-05-14,\n" +
            "            \"leaveReason\": 朋友结婚,\n" +
            "            \"leaveHowLong\": 2,\n" +
            "            \"type\": 通过\n" +
            "        }\n" +
            "    ],\n" +
            "    \"msg\": null,\n" +
            "    \"success\": true,\n" +
            "    \"failed\": false,\n" +
            "    \"error\": false\n" +
            "}";

    public AskLeaveMo(Context context) {
        this.context = context;
        queryList = new ArrayList<>();
        mGson = new Gson();
    }

    public List<AskForLeaveBean> getQueryList() {
        return queryList;
    }

    public void RequestData() {

//        HttpClient.getInstance().httpCheckWorkInfo(context, new ResultCallback() {
//            @Override
//            public void onFailure(Request request, Exception e) {
//                super.onFailure(request, e);
//            }
//
//            @Override
//            public void onError(int code, String message) {
//                super.onError(code, message);
//            }
//
//            @Override
//            public void onResponse(Object response) {
//                super.onResponse(response);
//            }
//
//            @Override
//            public void onResponse(int code, String msg) {
//                super.onResponse(code, msg);
//            }
//
//            @Override
//            public void onFinish() {
//                super.onFinish();
//            }
//
//            @Override
//            public void onStart() {
//                super.onStart();
//            }
//        });

        loadData();
    }

    public void RequestStudentData() {

        //模拟网络加载
        Message message = new Message();
        message.arg1 = 1;
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    public void RefreshData() {
        //模拟网络加载
        Message message = new Message();
        message.arg1 = 3;
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    private Gson mGson;

    private void loadData() {
        //模拟网络加载
        Message message = new Message();
        message.arg1 = 2;
        loadDataHandler.sendMessageDelayed(message, 1000);
    }

    private Handler loadDataHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.arg1) {
                case 1:
                    AskForLeaveListBean mResult1 = mGson.fromJson(strStudent, AskForLeaveListBean.class);
                    queryList = mResult1.getResult();
                    onFindAllDevicesListener.onFindSuccess();
                    break;
                case 2:
                    AskForLeaveListBean mResult = mGson.fromJson(str, AskForLeaveListBean.class);
                    queryList = mResult.getResult();
                    onFindAllDevicesListener.onFindSuccess();
                    break;
                case 3:
                    onFindAllDevicesListener.OnFindAllDataSuccess("");

                    break;
            }
        }
    };


    public void setOnFindAllDevicesListener(OnFindAllDataListener onFindAllDevicesListener) {
        this.onFindAllDevicesListener = onFindAllDevicesListener;
    }

    /**
     * 请求监听
     */
    public interface OnFindAllDataListener {
        void onFindSuccess();

        void OnFindAllDataSuccess(String strTitle);

        void onFailure();

        void onError();
    }

    class AskForLeaveListBean implements Serializable {
        private List<AskForLeaveBean> result;

        public List<AskForLeaveBean> getResult() {
            return result;
        }

        public void setResult(List<AskForLeaveBean> result) {
            this.result = result;
        }
    }
}
